package com.air.admin.controller.imp;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.air.admin.controller.IFunctionManagerController;
import com.air.admin.mo.AdminFunctionItem;
import com.air.admin.service.IAdminFunctionItemService;
import com.air.admin.vo.DataGridRequestVO;
import com.air.common.controller.imp.BaseControllerImp;
import com.air.common.util.BasicAppConstant;
import com.air.common.util.QueryCondition;
import com.air.common.util.QueryExpression;
import com.air.common.vo.PageResultListVO;
import com.air.common.vo.ResponseVO;

@Controller
@RequestMapping("/admin/functionManager")
public class FunctionManagerController extends BaseControllerImp implements
		IFunctionManagerController {
	
	@Autowired
	IAdminFunctionItemService adminFunctionItemService;
	
	@Override
	@RequestMapping("add")
	public ModelAndView addFunctionItem(HttpServletRequest request,
			HttpServletResponse response, AdminFunctionItem funItem)
			throws Exception {
		
		funItem = this.adminFunctionItemService.insert(funItem);
		ResponseVO responseVO = new ResponseVO();
		responseVO.addObject("funItem", funItem);
		return parse2Mav(request,responseVO);
		
	}

	@Override
	@RequestMapping("list")
	public ModelAndView listFunctionItem(HttpServletRequest request,
			HttpServletResponse response, DataGridRequestVO queryRequestVO)
			throws Exception {
		
		QueryCondition condition = new QueryCondition();
		
		if (queryRequestVO.getSort()!=null){
			condition.addOrderExpression(queryRequestVO.getSort(), queryRequestVO.getOrder());
		}
		PageResultListVO pageResult =  this.adminFunctionItemService.queryByCondition(condition, queryRequestVO.getPage()-1, queryRequestVO.getRows());
		
		ModelAndView mav = new ModelAndView();
		mav.addObject(BasicAppConstant._KEY_RESPONSE_VO,JSONObject.fromObject(pageResult).toString());
		mav.setViewName(BasicAppConstant._VIEW_COMMON_JSON);
		
		return mav;
		
	}

	@Override
	@RequestMapping("modify")
	public ModelAndView modifyFunctionItem(HttpServletRequest request,
			HttpServletResponse response, AdminFunctionItem funItem)
			throws Exception {
		
		this.adminFunctionItemService.updateById(funItem);
		
		ResponseVO responseVO = new ResponseVO();
		responseVO.addObject("funItem", funItem);
		return parse2Mav(request,responseVO);
	}

	@Override
	@RequestMapping("remove")
	public ModelAndView removeFunctionItems(HttpServletRequest request,
			HttpServletResponse response, String idList) throws Exception {
		
		List<String> funItemIdList= Arrays.asList(idList.split(","));
		QueryCondition condition = new QueryCondition();
		condition.addQueryCondition("id", funItemIdList, QueryExpression.IN);
		this.adminFunctionItemService.deleteByCondition(condition);
		ResponseVO responseVO = new ResponseVO();
		
		return parse2Mav(request,responseVO);
	}

}
